package Simulation;

public class ServiceCompletion extends SimEvent {

	public static long counter = 0;

	private Customer customerGettingServed;

	public ServiceCompletion(long time, Customer customer) {
		super(time);
		customerGettingServed = customer;
	}

	@Override
	public void process() {
		counter++;
		SimState state = SimState.instance;
		// Set the completion time of the customer in service
		customerGettingServed.setServiceCompletion(state.now);
		state.handleCustomerStatistics(customerGettingServed);
		// In the case that the queueSize is larger than 0
		if (state.queueSize > 0) {
			// get the next customer in the queue
			Customer customerInService = state.waiting_queue.poll();
			// Decrease the queueSize by one
			state.decreaseQueue();
			// The customer is starting to get served
			customerInService.setService_init_time(state.now);
			// Create a new ServiceCompletion event and insert it in the
			// EventQueue
			state.ec.add(new ServiceCompletion(state.now
					+ state.getNextServiceCompletionTime(), customerInService));
			// If we have customer deadlines, we need to remove them as soon as
			// the customer service begins
			if (DES.A3_E2) {
				state.ec.remove(customerGettingServed.expirationEvent);
			}
			// We also count the customer as satisfied, as his deadline did not
			// expire
			state.deadline_expiration.count(0);

		} else {
			// Set the state of a busy server to idle
			state.increaseFreeServers();
		}
	}
}
